
# Climat 

# Data preparation ----

gc()

## Set wd and load data
setwd("")
load("panel_ch_june_23.RData")

## Load packages
library(sjmisc)
library(tidyverse)
source("function_plot_agr.R")

# Take a glimpse, select variables of interest, remove labels, frequencies
glimpse(ch23)

df <- ch23 %>% select(vint_climat_w2, vchoice_climat_w2, particip_w2, 
                      party_cue, pid_close_w1, pid_first_w1, pid_remind_w1, pid_remind_first_w1,
                      imp_climat_w2, complex_climat_w2,
                      genknow_a_w1, genknow_b_w1, genknow_c_w1, genknow_d_w1,  
                      arg_climat_a_w2, arg_climat_b_w2, arg_climat_c_w2,
                      arg_climat_d_w2, arg_climat_e_w2, arg_climat_f_w2, 
                      party_reco_climat_w2, gov_reco_climat_w2, 
                      intgrp_ecosuis_climat_w2, intgrp_usam_climat_w2,
                      intgrp_uss_climat_w2, day_itw_w2, month_itw_w2,
                      isknow_climat_a_w2, isknow_climat_b_w2, isknow_climat_c_w2,
                      starts_with("att"), -att_campaign_w3,
                      gender, age, educ_w1, region, received_w2, already_vote_w2)
                      
df <- labelled::remove_labels(df)
rm(ch23_open)



## Vote intention/choice ----
frq(df$vint_climat_w2)
frq(df$vchoice_climat_w2)

df <- df %>%
  mutate(votew2 = case_when(
    vint_climat_w2 == "certainly favor" | vint_climat_w2 == "likely favor" | vchoice_climat_w2 == "yes" ~ "yes",
    vint_climat_w2 == "certainly against" | vint_climat_w2 == "likely against" | vchoice_climat_w2 == "no" ~ "no",
    vint_climat_w2 == "dk" | vchoice_climat_w2 == "dk" ~ "dk"),
         vote_w2_int_choice = case_when(
    vint_climat_w2 == "certainly favor" | vint_climat_w2 == "likely favor" ~ "int yes",
    vchoice_climat_w2 == "yes" ~ "choice yes",
    vint_climat_w2 == "certainly against" | vint_climat_w2 == "likely against" ~ "int no",
    vchoice_climat_w2 == "no" ~ "choice no",
    vint_climat_w2 == "dk" | vchoice_climat_w2 == "dk" ~ "dk"))

frq(df$votew2)
xtabs(~ vint_climat_w2 + votew2, data = df, subset = NULL)
xtabs(~ vchoice_climat_w2 + votew2, data = df, subset = NULL)

frq(df$vote_w2_int_choice)
xtabs(~ vint_climat_w2 + vote_w2_int_choice, data = df, subset = NULL)
xtabs(~ vchoice_climat_w2 + vote_w2_int_choice, data = df, subset = NULL)


## Merge day / week of itw ----
frq(df$day_itw_w2)
frq(df$month_itw_w2)

xtabs(~ day_itw_w2 + month_itw_w2, data = df, subset = NULL)

df <- df %>% mutate(date_itw = paste(as.character(day_itw_w2), as.character(month_itw_w2), "2023", sep = "-"))

df$date_itw <- as.Date(df$date_itw, format = "%d-%m-%Y")

frq(df$date_itw)

df <- df %>% mutate(days = 41-(as.numeric(difftime(as.Date("2023-06-18"), date_itw, units = "days"))))

frq(df$days)
xtabs(~ date_itw + days, data = df, subset = NULL)

### Number of observations / each day 
freq_table <- as.data.frame(table(df$days, useNA = "no"))
N_values <- freq_table$Freq
mean(N_values)
sd(N_values)

rm(freq_table,N_values)


## Already voted or not 
xtabs(~ already_vote_w2 + received_w2, data = df, addNA = T, subset = NULL)

df <- df %>%
  mutate(received_w2 = case_when(
    received_w2 == 1 ~ "yes",
    received_w2 == 2 ~ "no",
    received_w2 == 99 ~ "nsp",
    TRUE ~ NA), 
    already_vote_w2 = case_when(
      already_vote_w2 == 1 ~ "yes",
      already_vote_w2 == 2 ~ "no",
      TRUE ~ NA),
    votemail = case_when(
      received_w2 %in% c("no","nsp") ~ "not receive",
      received_w2 == "yes" & already_vote_w2 == "no" ~ "receive not vote",
      received_w2 == "yes" & already_vote_w2 == "yes" ~ "receive and vote",
      TRUE ~ NA))

df <- df %>% mutate(filter_particip_profile = 
                      case_when(votemail == "receive and vote" | particip_w2 == "certainly yes" ~ 1,
                                is.na(days) ~ NA,
                                TRUE ~ 0))

frq(df$filter_particip_profile)
df %>% select(days, received_w2, already_vote_w2, particip_w2, votemail, vote_w2_int_choice, votew2, filter_particip_profile) %>% print(n = 50)


## Arguments ----
col_names <- c("arg_climat_a_w2", "arg_climat_b_w2", "arg_climat_c_w2", "arg_climat_d_w2", "arg_climat_e_w2", "arg_climat_f_w2")

df <- df %>%
  mutate(across(c(arg_climat_a_w2, arg_climat_b_w2, arg_climat_c_w2),
                  ~ case_when(. == "strong agree" ~ 2, . == "agree" ~ 1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ -1, . == "strong disagree" ~ -2),
                              .names = "{col}_rcd")) %>% 
  mutate(across(c(arg_climat_d_w2, arg_climat_e_w2, arg_climat_f_w2),
                  ~ case_when(. == "strong agree" ~ -2, . == "agree" ~ -1,
                              . == "dk" ~ 0,
                              . == "disagree" ~ 1, . == "strong disagree" ~ 2),
                              .names = "{col}_rcd")) %>% 
  mutate(arg_pro_climat_w2 = arg_climat_a_w2_rcd + arg_climat_b_w2_rcd + arg_climat_c_w2_rcd,
         arg_con_climat_w2 = arg_climat_d_w2_rcd + arg_climat_e_w2_rcd + arg_climat_f_w2_rcd,
         arg_full_climat_w2 = arg_pro_climat_w2 + arg_con_climat_w2) %>% 
  mutate(arg_n_dk = case_when(rowSums(!is.na(across(all_of(col_names)))) == 0 ~ NA_real_,
                              T ~ rowSums(across(all_of(col_names)) == "dk", na.rm = T)))

rm(col_names)


## PID ----
df %>% select(starts_with("pid")) %>% frq()
xtabs(~ pid_close_w1 + pid_first_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_w1, data = df, subset = NULL)
xtabs(~ pid_close_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_remind_w1 + pid_remind_first_w1, data = df, subset = NULL)
xtabs(~ pid_first_w1 + pid_remind_first_w1, data = df, subset = NULL)


df <- df %>% mutate(pid = 
                      case_when(pid_first_w1 == "PLR" | pid_remind_first_w1 == "PLR" ~ "PLR",
                                pid_first_w1 == "Le Centre" | pid_remind_first_w1 == "Le Centre" ~ "Le Centre",
                                pid_first_w1 == "PS" | pid_remind_first_w1 == "PS" ~ "PS",
                                pid_first_w1 == "UDC" | pid_remind_first_w1 == "UDC" ~ "UDC",
                                pid_first_w1 == "PES" | pid_remind_first_w1 == "PES" ~ "PES",
                                pid_first_w1 == "PVL" | pid_remind_first_w1 == "PVL" ~ "PVL",
                                pid_first_w1 %in% c("Lega","MCG","PCS","PEV","UDF","PST-POP") | pid_remind_first_w1 %in% c("Lega","MCG","PCS","PEV","UDF","PST-POP") ~ "Other",
                                pid_remind_first_w1 == "dk" ~ "noPID",
                                pid_close_w1 == "no" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "dk" & (pid_remind_w1 == "no" | pid_remind_w1 == "dk") ~ "noPID",
                                pid_close_w1 == "yes" & pid_remind_w1 == "no" ~ "noPID"))

frq(df$pid)
df %>% select(starts_with("pid")) %>% print(n=50)



## Appendix A ----
dfsample1 <- df

frq(dfsample1$gender)
frq(dfsample1$age)
frq(dfsample1$educ_w1)

dfsample1 <- dfsample1 %>% mutate(age_rcd = case_when(age >= 18 & age <= 24 ~ "18-24",
                                        age >= 25 & age <= 34 ~ "25-34",
                                        age >= 35 & age <= 44 ~ "35-44",
                                        age >= 45 & age <= 54 ~ "45-54",
                                        age >= 55 & age <= 65 ~ "55-65",
                                        age >= 66 ~ "66+"),
                    educ_rcd = case_when(educ_w1 %in% c("Ecole obligatoire","Apprentissage","Maturite","Brevet/diplome federal") ~ "notuni",
                                         educ_w1 %in% c("Ecole technique/pro sup.","HES","Universite/EPF") ~ "uni"))

dfsample2 <- dfsample1 %>% drop_na(date_itw)

frq(dfsample1$gender)
frq(dfsample2$gender)

frq(dfsample1$age_rcd)
frq(dfsample2$age_rcd)

frq(dfsample1$educ_rcd)
frq(dfsample2$educ_rcd)

rm(dfsample1, dfsample2)


## Knowledge party cues ----
frq(df$pid)
df <- df %>% filter(pid %in% c("PLR","Le Centre","PS","UDC","PES","PVL","noPID"))
frq(df$pid)

frq(df$party_reco_climat_w2) 
xtabs(~ pid + party_reco_climat_w2, data = df, subset = NULL)
addmargins(xtabs(~ pid + party_reco_climat_w2, data = df, subset = NULL))

df <- df %>% 
  mutate(party_reco_climat_w2_know = case_when(
    pid %in% c("PLR","Le Centre","PS","PES","PVL") & party_reco_climat_w2 == "yes" ~ "correct",
    pid == "UDC" & party_reco_climat_w2 == "no" ~ "correct",
    pid %in% c("PLR","Le Centre","PS","PES","PVL") & party_reco_climat_w2 %in% c("no","free","dk") ~ "not",
    pid == "UDC" & party_reco_climat_w2 %in% c("yes","free","dk") ~ "not",
    pid == "noPID" ~ "noPID"))
    
frq(df$party_reco_climat_w2_know) 

# Group "free" with "dk" (knowledge of cues)
df <- df %>% 
  mutate(across(c(party_reco_climat_w2),
                    ~ case_when(. == "yes" ~ "yes",
                                . == "no" ~ "no",
                                . == "free" | . == "dk" ~ "free/dk"),
                                .names = "{col}_rcd"))

frq(df$party_reco_climat_w2_rcd)


## Importance ----
frq(df$imp_climat_w2)
df$imp_climat_w2[df$imp_climat_w2 == 99] <- NA
frq(df$imp_climat_w2)

df <- df %>% mutate(imp_climat_w2_bi = case_when(
  imp_climat_w2 < 6 ~ 0,
  imp_climat_w2 >= 6 ~ 1,
  TRUE ~ NA))  

xtabs(~ imp_climat_w2 + imp_climat_w2_bi, data = df, subset = NULL)


## IS knowledge ----
frq(df$isknow_climat_a_w2)
frq(df$isknow_climat_b_w2)
frq(df$isknow_climat_c_w2)

df <- df %>%
  mutate(isknow_climat_a_w2_rcd = ifelse(is.na(isknow_climat_a_w2), NA, as.numeric(grepl("correct", tolower(isknow_climat_a_w2)))),
         isknow_climat_b_w2_rcd = ifelse(is.na(isknow_climat_b_w2), NA, as.numeric(grepl("correct", tolower(isknow_climat_b_w2)))),
         isknow_climat_c_w2_rcd = ifelse(is.na(isknow_climat_c_w2), NA, as.numeric(grepl("correct", tolower(isknow_climat_c_w2)))))

frq(df$isknow_climat_a_w2_rcd)
frq(df$isknow_climat_b_w2_rcd)
frq(df$isknow_climat_c_w2_rcd)

df <- df %>% mutate(isknow_climat_scale = isknow_climat_a_w2_rcd + isknow_climat_b_w2_rcd + isknow_climat_c_w2_rcd) %>% 
             mutate(isknow_climat_scale_rcd = case_when(isknow_climat_scale == 0 ~ 0,
                                                        isknow_climat_scale == 1 ~ 1,
                                                        isknow_climat_scale == 2 | isknow_climat_scale == 3 ~ 2)) %>% 
             mutate(isknow_climat_scale_bi = case_when(isknow_climat_scale == 0 | isknow_climat_scale == 1 ~ 0,
                                                       isknow_climat_scale == 2 | isknow_climat_scale == 3 ~ 1))
             
frq(df$isknow_climat_scale)
frq(df$isknow_climat_scale_rcd)
frq(df$isknow_climat_scale_bi)



## Select var and remove NA ----

df <- df %>% 
  select(isknow_climat_scale, party_reco_climat_w2_know, arg_n_dk, arg_full_climat_w2, filter_particip_profile, 
         votemail, date_itw, days, votew2, pid, imp_climat_w2, gender, age, educ_w1,
         arg_climat_a_w2_rcd, arg_climat_b_w2_rcd, arg_climat_c_w2_rcd, arg_climat_d_w2_rcd, arg_climat_e_w2_rcd, arg_climat_f_w2_rcd) %>% 
  drop_na()


## Recode mediators/outcomes ----

### Consistent voting ----
df <- df %>% mutate(
  voteinlinearg = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_climat_w2 <= -3 & votew2 == "no" ~ "inline",
                            arg_full_climat_w2 <= -3 & votew2 == "yes" ~ "not inline",
                            arg_full_climat_w2 >= -2 & arg_full_climat_w2 <= 2 ~ "ambivalent",
                            arg_full_climat_w2 >= 3 & votew2 == "no" ~ "not inline",
                            arg_full_climat_w2 >= 3 & votew2 == "yes" ~ "inline"),
  voteinlinearg0 = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_climat_w2 <= -1 & votew2 == "no" ~ "inline",
                            arg_full_climat_w2 <= -1 & votew2 == "yes" ~ "not inline",
                            arg_full_climat_w2 == 0 ~ "ambivalent",
                            arg_full_climat_w2 >= 1 & votew2 == "no" ~ "not inline",
                            arg_full_climat_w2 >= 1 & votew2 == "yes" ~ "inline"),
  voteinlinearg5 = case_when(votew2 == "dk" ~ "undecided",
                            arg_full_climat_w2 <= -6 & votew2 == "no" ~ "inline",
                            arg_full_climat_w2 <= -6 & votew2 == "yes" ~ "not inline",
                            arg_full_climat_w2 >= -5 & arg_full_climat_w2 <= 5 ~ "ambivalent",
                            arg_full_climat_w2 >= 6 & votew2 == "no" ~ "not inline",
                            arg_full_climat_w2 >= 6 & votew2 == "yes" ~ "inline"),
  voteinlinearg_rcd = case_when(voteinlinearg == "inline" ~ 1,
                                voteinlinearg %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg0_rcd = case_when(voteinlinearg0 == "inline" ~ 1,
                                voteinlinearg0 %in% c("ambivalent","not inline","undecided") ~ 0),
  voteinlinearg5_rcd = case_when(voteinlinearg5 == "inline" ~ 1,
                                voteinlinearg5 %in% c("ambivalent","not inline","undecided") ~ 0))
  


### Knowledge cue and vote inline ----
df <- df %>% mutate(party_reco_climat_w2_know_rcd = case_when(party_reco_climat_w2_know == "correct" ~ 1,
                                                              party_reco_climat_w2_know == "noPID" | party_reco_climat_w2_know == "not" ~ 0),
                    voteinlineparty = case_when(pid %in% c("PLR","Le Centre","PS","PES","PVL") & votew2 == "yes" ~ "inline",
                                                pid %in% c("PLR","Le Centre","PS","PES","PVL") & votew2 == "no" ~ "not inline",
                                                pid %in% c("PLR","Le Centre","PS","PES","PVL") & votew2 == "dk" ~ "undecided",
                                                pid == "UDC" & votew2 == "no" ~ "inline",
                                                pid == "UDC" & votew2 == "yes" ~ "not inline",
                                                pid == "UDC" & votew2 == "dk" ~ "undecided",
                                                pid == "noPID" ~ "noPID"),
                    voteinlineparty_rcd = case_when(voteinlineparty == "inline" ~ 1,
                                                    voteinlineparty %in% c("not inline", "undecided", "noPID") ~ 0))

frq(df$voteinlineparty)


## Remove 18th June (8 obs) ----
df <- df %>% filter(date_itw != "2023-06-18")

## Filter only party identifiers ---- 
df <- df %>% filter(pid != "noPID")
frq(df$pid)


# Descriptive / appendix A ----
frq(df$votemail) 
frq(df$filter_particip_profile) 

frq(df$party_reco_climat_w2_know_rcd)

ggplot(df) + 
  aes(x = arg_full_climat_w2) +
  geom_density(fill = "grey", alpha = 0.5) +
  theme_minimal() +
  scale_x_continuous(limits = c(-12, 12), breaks = seq(-12, 12, by = 2)) + 
  xlab("Climate - argument scale") +
  ylab("Density")

ggsave("scale_climat.png", scale = 1, width = 12, height = 6, units = "cm", dpi = 600)

avg_dk_climat <- mean(df$arg_n_dk)
round(avg_dk_climat,2)
sd_dk_climat <- sd(df$arg_n_dk)
round(sd_dk_climat,2)

frq(df$voteinlineparty_rcd)
frq(df$voteinlinearg_rcd)

addmargins(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL))
cross_climat <- as.data.frame(questionr::prop(xtabs(~ voteinlineparty_rcd + voteinlinearg_rcd, data = df, subset = NULL)))
cross_climat <- cross_climat %>% mutate(ballot = "climat")
cross_climat


# Alpha arguments
df %>% 
  select(arg_climat_a_w2_rcd, arg_climat_b_w2_rcd, arg_climat_c_w2_rcd) %>% 
  psych::alpha()  

df %>% 
  select(arg_climat_d_w2_rcd, arg_climat_e_w2_rcd, arg_climat_f_w2_rcd) %>% 
  psych::alpha() 




# Analysis - aggregate ----

## 2a ----
cat_ols(df, "party_reco_climat_w2_know_rcd", ballot = "climat")

p01_climat <- graf

p01_climat <- p01_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "", title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                                                       values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p01_climat

magr <- lm(percentage ~ days * party_reco_climat_w2_know_rcd, data = dfagr)
summary(magr)

magr_know_cue <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                "(Intercept)" = "Intercept", "days" = "Days",
                "party_reco_climat_w2_know_rcd1" = "Knowledge party cue",
                "days:party_reco_climat_w2_know_rcd1" = "Days × Knowledge party cue"),
    estimate  = round(estimate, 2),
    std.error = round(std.error, 2),
    sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 


## 2b ----
frq(df$arg_n_dk)

num_ols(df, "arg_n_dk", ballot = "climat")
p02_climat <- graf

p02_climat <- p02_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "", title = "Climate") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p02_climat

magr <- lm(avg ~ days, data = dfagr)
summary(magr)

magr_dk <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term, "(Intercept)" = "Intercept", "days" = "Days"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig)  




## 2c ----
frq(df$voteinlineparty_rcd)

cat_ols(df, "voteinlineparty_rcd", ballot = "climat")
p04_climat <- graf

p04_climat <- p04_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p04_climat

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)

magr_inline <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlineparty_rcd1" = "Inline",
                       "days:voteinlineparty_rcd1" = "Days × Inline"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 




## 2d ----
frq(df$voteinlinearg_rcd)

cat_ols(df, "voteinlinearg_rcd", ballot = "climat")
p03_climat <- graf

p03_climat <- p03_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

p03_climat

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

magr_cons <- broom::tidy(magr) %>% select(term, estimate, std.error, p.value) %>%
  mutate(term = recode(term,
                       "(Intercept)" = "Intercept", "days" = "Days",
                       "voteinlinearg_rcd1" = "Consistent voting",
                       "days:voteinlinearg_rcd1" = "Days × Consistent voting"),
         estimate  = round(estimate, 2),
         std.error = round(std.error, 2),
         sig       = ifelse(p.value < 0.05, "p<0.05", "ns")) %>%
  select(-p.value) %>%
  rename(Variable = term, Coefficient = estimate, SE = std.error, Sig = sig) 





## Appendix B1 ----
writexl::write_xlsx(list(
    "2a" = magr_know_cue, "2b" = magr_dk,
    "2c" = magr_inline, "2d" = magr_cons),
  "magr_climat.xlsx")

rm(graf, magr, dfagr)



# Analysis - individual ---- 

glimpse(df)

colsss <- c("gender", "voteinlineparty_rcd", "voteinlinearg_rcd", "voteinlinearg0_rcd", "voteinlinearg5_rcd")
df[colsss] <- lapply(df[colsss], as.factor)

colsss <- c("party_reco_climat_w2_know_rcd", "isknow_climat_scale", "arg_n_dk", "days", "imp_climat_w2", "age", "educ_w1")
df[colsss] <- lapply(df[colsss], as.numeric)
df[colsss] <- lapply(df[colsss], scales::rescale)

rm(colsss)


## Appendix B2 ----

library(lavaan)

glimpse(df)

df$party_reco_climat_w2_know_rcd <- as.factor(df$party_reco_climat_w2_know_rcd)
df$gender <- as.numeric(df$gender)

model <- '
  # Days -> cue -> vote inline
  party_reco_climat_w2_know_rcd ~ a1*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_climat_w2_know_rcd + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlinearg_rcd ~ c2*days + b2*arg_n_dk + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_climat_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_climat <- sem(model, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_climat_w2_know_rcd"))

sumfit_climat <- summary(fit_climat, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_climat_df <- sumfit_climat[["pe"]]


# Robustness ----

## Model for figure C3a.3 ----

model0 <- '
  # Days -> cue -> vote inline
  party_reco_climat_w2_know_rcd ~ a1*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_climat_w2_know_rcd + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlinearg0_rcd ~ c2*days + b2*arg_n_dk + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_climat_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg0_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_climat0 <- sem(model0, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg0_rcd","party_reco_climat_w2_know_rcd"))

sumfit_climat0 <- summary(fit_climat0, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_climat0_df <- sumfit_climat0[["pe"]]


## Model for figure C3b.3 ----

model5 <- '
  # Days -> cue -> vote inline
  party_reco_climat_w2_know_rcd ~ a1*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlineparty_rcd ~ c1*days + b1*party_reco_climat_w2_know_rcd + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age   

  # Days -> dk arg -> consistent
  arg_n_dk ~ a2*days + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  
  voteinlinearg5_rcd ~ c2*days + b2*arg_n_dk + isknow_climat_scale + imp_climat_w2 + educ_w1 + gender + age  

  # Covariances
  party_reco_climat_w2_know_rcd ~~ arg_n_dk
  voteinlineparty_rcd ~~ voteinlinearg5_rcd

  # Indirect
  days_cue_inline := a1*b1
  days_dk_cons := a2*b2
  
  # Total
  tot_inline := c1 + (a1*b1) 
  tot_cons := c2 + (a2*b2) 
'

fit_climat5 <- sem(model5, data = df, ordered = c("voteinlineparty_rcd","voteinlinearg5_rcd","party_reco_climat_w2_know_rcd"))

sumfit_climat5 <- summary(fit_climat5, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_climat5_df <- sumfit_climat5[["pe"]]




## Figure C3a.2d ----

frq(df$voteinlinearg0_rcd)

cat_ols(df, "voteinlinearg0_rcd", ballot = "climat")
pzero_climat <- graf

pzero_climat[["data"]][["days"]] <- pzero_climat[["data"]][["days"]] * 40

pzero_climat <- pzero_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pzero_climat

magr <- lm(percentage ~ days * voteinlinearg0_rcd, data = dfagr)
summary(magr)

rm(graf)



## Figure C3b.2d ----

frq(df$voteinlinearg5_rcd)

cat_ols(df, "voteinlinearg5_rcd", ballot = "climat")
pfive_climat <- graf

pfive_climat[["data"]][["days"]] <- pfive_climat[["data"]][["days"]] * 40

pfive_climat <- pfive_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pfive_climat

magr <- lm(percentage ~ days * voteinlinearg5_rcd, data = dfagr)
summary(magr)

rm(graf)



## Appendix C1 ----

df_certain_voter <- df %>% filter(filter_particip_profile == 1)

### Figure C1.2a ----
cat_ols(df_certain_voter, "party_reco_climat_w2_know_rcd", ballot = "climat")

pcertain01_climat <- graf

pcertain01_climat[["data"]][["days"]] <- pcertain01_climat[["data"]][["days"]] * 40

pcertain01_climat <- pcertain01_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not correct","Correct"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain01_climat

magr <- lm(percentage ~ days * party_reco_climat_w2_know_rcd, data = dfagr)
summary(magr)


### Figure C1.2b ----
frq(df_certain_voter$arg_n_dk)

num_ols(df_certain_voter, "arg_n_dk", ballot = "climat")
pcertain02_climat <- graf

pcertain02_climat[["data"]][["days"]] <- pcertain02_climat[["data"]][["days"]] * 40
pcertain02_climat[["data"]][["avg"]] <- pcertain02_climat[["data"]][["avg"]] * 6

pcertain02_climat <- pcertain02_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = avg), method = "lm", alpha = 0.2, linewidth = 0.5, color = "black") +
  labs(x = "Days before voting day", y = "Average", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  scale_y_continuous(breaks = seq(0, 2, by = 0.2), limits = c(0,2)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain02_climat

magr <- lm(avg ~ days, data = dfagr)
summary(magr)



### Figure C1.2c ----
frq(df_certain_voter$voteinlineparty_rcd)

cat_ols(df_certain_voter, "voteinlineparty_rcd", ballot = "climat")
pcertain04_climat <- graf

pcertain04_climat[["data"]][["days"]] <- pcertain04_climat[["data"]][["days"]] * 40

pcertain04_climat <- pcertain04_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not inline","Inline"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain04_climat

magr <- lm(percentage ~ days * voteinlineparty_rcd, data = dfagr)
summary(magr)


### Figure C1.2d ----
frq(df_certain_voter$voteinlinearg_rcd)

cat_ols(df_certain_voter, "voteinlinearg_rcd", ballot = "climat")
pcertain03_climat <- graf

pcertain03_climat[["data"]][["days"]] <- pcertain03_climat[["data"]][["days"]] * 40

pcertain03_climat <- pcertain03_climat +
  geom_point(shape = 16, size = 1, alpha = 0.75) +
  geom_smooth(aes(y = percentage), method = "lm", alpha = 0.2, linewidth = 0.5) +
  labs(x = "Days before voting day", y = "Percentage", color = "", fill = "",
       title = "Climate") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                     values = c("0" = "grey70", "1" = "grey30")) +
  scale_fill_manual(breaks = c("0","1"), labels = c("Not consistent","Consistent"),
                    values = c("0" = "grey70", "1" = "grey30")) +
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1, 11, 21, 31, 40), labels = c("-40", "-30", "-20", "-10", "0"))

pcertain03_climat

magr <- lm(percentage ~ days * voteinlinearg_rcd, data = dfagr)
summary(magr)

rm(graf)

### Results for figure C1.3b ----
fit_climatcertain <- sem(model, data = df_certain_voter, ordered = c("voteinlineparty_rcd","voteinlinearg_rcd","party_reco_climat_w2_know_rcd"))

sumfit_climatcertain <- summary(fit_climatcertain, fit.measures = T, standardized = F, rsquare = T, ci = T)
sumfit_climatcertain_df <- sumfit_climatcertain[["pe"]]

rm(model, model0, model5, magr, dfagr)

